﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using Core.DAL;

namespace Core
{
    public partial class NotificationQueue
    {
        public static NotificationQueue IncrementTimesSent(DAL.DataAccessLayer dc, Guid expenseid, Guid depid)
        {
            NotificationQueue eq = null;
            
            //Obter a linha de queue e fazer update do n de vezes enviado
            var q = from queue in dc.DataContext.NotificationQueues
                    where queue.ExpenseLineId.Equals(expenseid) && queue.DepartmentLevelId.Equals(depid)
                    select queue;

            eq = q.Single<NotificationQueue>();
            ++eq._N_times_sent;
            eq._Modified_at = DateTime.Now;
            dc.DataContext.SubmitChanges();            
            return eq;
        }

        public static void Insert(DAL.DataAccessLayer dc, ExpenseLine line, DepartmentLevel dl)
        {
            NotificationQueue eq = new NotificationQueue();

            eq.ExpenseLineId = line.Id;
            eq.DepartmentLevelId = dl.Id;
            eq.N_times_sent = 0;
            eq.Created_at = DateTime.Now.ToLocalTime();
            
            dc.DataContext.NotificationQueues.InsertOnSubmit(eq);            
        }

        public static NotificationQueue[] Get(DAL.DataAccessLayer dc, Guid expense_line_id, Guid department_level_id)
        {
            
            var query = from queue in dc.DataContext.NotificationQueues
                        where queue.ExpenseLineId.Equals(expense_line_id) && queue.DepartmentLevelId.Equals(department_level_id)
                        select queue;

            return query.ToArray<NotificationQueue>();       

        }

        public static NotificationQueue[] Get(DAL.DataAccessLayer dc, Guid department_level_id, bool closed)
        {            
            var query = from queue in dc.DataContext.NotificationQueues
                        where queue.DepartmentLevelId.Equals(department_level_id) && queue.Closed == closed
                        select queue;

            return query.ToArray<NotificationQueue>();            
        }

        public static void CloseElementsOnQueue(DAL.DataAccessLayer dc, Guid expenseid)
        {
            var query = from queue in dc.DataContext.NotificationQueues
                        where queue.ExpenseLineId.Equals(expenseid)
                        select queue;
            foreach (NotificationQueue nq in query.ToArray<NotificationQueue>())
                nq.Closed = true;

            dc.DataContext.SubmitChanges();            
        }
        public static void CloseElementOnQueue(DAL.DataAccessLayer dcx, Guid expense_id, Guid department_id)
        {          
 
            NotificationQueue nq = dcx.DataContext.NotificationQueues.SingleOrDefault(elem => elem.ExpenseLineId.Equals(expense_id) && elem.DepartmentLevelId.Equals(department_id));
            if (nq != null)
                nq.Closed = true;
            dcx.DataContext.SubmitChanges();
            
        }
    }
}
